<?php
/**
 * User: shaopu
 * Date: 2020-05-7
 * Time: 18:25
 */
namespace Frieza\Modules\ConfManager\Controllers;



use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Form;
use Doraemon\cache\Ad2baba;

class Ad2babaController extends Controller
{
	public function index(Content $content, Request $request){

		$conf_api_rs = json_decode(file_get_contents('http://conf.be-micro-service/conf?type=inner&project_code=inner:202005:ad2baba') , 1);
		$confs = json_decode($conf_api_rs['data']['inner:202005:ad2baba']['conf'] , 1);
		$project_names = [];
		if(!empty($confs)){
			foreach ($confs['projects'] as $key => $value) {
				$project_names[$value['project_name'].'--'.$value['second_name']] = $value['project_owner'];
			}
		}
		

		foreach (array_keys($project_names) as $name) {
			$cnt = Ad2baba::getByProjectname($name);
            list($project_name , $second_name) = explode('--' , $name);
			$rows [] = [
				'project_name' => $project_name,
                'second_name' => $second_name,
				'count' => $cnt,
				'project_owner' => $project_names[$name],
			];
		}


        $tab = new Tab();
        $tab->add('当前同步数', $this->list_running($rows), true);
        $content->row($tab);
        return $content
            ->header('配置管理')
            ->breadcrumb(['text' => '配置管理']);
	}

	private function list_running($list){
		$gridModel = new pGridDataModel($list);

        $admin_name = Admin::user()->name;

        $is_administrator = Admin::user()->isAdministrator();

        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel , $admin_name , $is_administrator) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableFilter();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->column("project_name", '项目名称');
                $grid->column("second_name", '二级名称');
                $grid->column("project_owner", '负责人');
                $grid->column("count", '当前数量');
                $grid->actions(function (Grid\Displayers\Actions $actions) use ($admin_name , $is_administrator) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->append('<a href="/admin/confManager/ad2baba/detail/'.$actions->row['project_name'].'" class="fa">查看详细</a>');

                });
            });

        return $gridList->render();
	}

    public function detail(Content $content, Request $request , $project_name){
        

        $data = Ad2baba::getDetailByProjectname($project_name);


        $tab = new Tab();
        $tab->add('10分钟统计', $this->_detail_by_minute($data), true);
        $tab->add('按小时统计', $this->_detail_by_hour($data), true);
        $content->row($tab);
        return $content
            ->header('配置管理')
            ->breadcrumb(['text' => '配置管理']);
    }
    private function _detail_by_minute($data){
        foreach ($data as $key => $value) {
            $html .= $key.": ".$value."\n<br/>";
        }
        return $html;
    }
    private function _detail_by_hour($data){
        $js = <<<EOT
        var myChart = echarts.init(document.getElementById('chartByHours'));
option = {
    xAxis: {
        type: 'category',
        data: [{hours}]
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [{values}],
        type: 'bar'
    }]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

EOT;
        $hour2value = array();
        //按小时加总
        foreach ($data as $key => $value) {
            $hour = substr($key , 9,2);
            $hour2value[$hour]+=$value;
        }
        $js = str_replace('{hours}' , "'".implode("','" , array_keys($hour2value))."'" , $js);
        $js = str_replace('{values}' , "'".implode("','" , array_values($hour2value))."'" , $js);

        admin::js('https://cdn.jsdelivr.net/npm/echarts@4/dist/echarts.min.js');
        admin::script($js);
        return '<div id="chartByHours" style="width: 1200px;height:400px;">sfadf</div>';
    }
}